<?php

namespace App\Admin\Controllers\Agent;

use App\Events\AgentCreated;
use App\model\agent\Agent;
use App\model\agent\AgentDepartment;
use App\model\agent\AgentPositon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Storage;

class AgentController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '经纪人管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Agent);
        $grid->column('id', 'Id');
        $grid->column('name', '姓名');
        $grid->column('mobile', '手机号');
        $grid->column('department.name', '部门');
        $grid->column('position.name', __('职位'));
        $grid->column('avatar', '头像')->image('',100,100);
        $grid->column('header_address', '总部地址');
        $grid->column('address', '地址');
        $grid->column('qrcode','小程序码')->display(function ($value){
            return   Storage::url($value);
        })->image('','100','100');
        $grid->column('file','下载二维码')->display(function ($value){
            return Storage::url($this->qrcode);
        })->downloadable();
        $grid->column('created_at', '创建时间');
        $grid->actions(function (Grid\Displayers\Actions $actions){
            $actions->disableView();
        });
        $states = [
            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'warning'],
        ];
        $grid->column('status')->switch($states);
        $grid->filter(function (Grid\Filter $filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('name', '姓名');
            $filter->equal('mobile','手机号')->mobile();
            $filter->like('mobile', '总部地址');
            $filter->like('mobile', '地址');

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Agent::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('mobile', __('Mobile'));
        $show->field('name', __('Name'));
        $show->field('department_id', __('Department id'));
        $show->field('position_id', __('Position id'));
        $show->field('avatar', __('Avatar'));
        $show->field('header_address', __('Header address'));
        $show->field('address', __('Address'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Agent);


        $form->text('name','姓名' )->required();
        $form->image('avatar', '头像')->required();
        $form->mobile('mobile', '手机号')->required();
        $form->radio('department_id', '部门')->options(AgentDepartment::getSelectOptions())->required();
        $form->radio('position_id', '职位')->options(AgentPositon::getSelectOptions())->required();
        $states = [
            'on'  => ['value' => 1, 'text' => '总店地址', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '分店地址', 'color' => 'warning'],
        ];
        $form->switch('is_zong','地址选项')->states($states)->default(0);
        $form->text('address', '地址');


        $states = [
            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'warning'],
        ];
        $form->switch('status','状态')->states($states)->default(1);
        $states = [
            'on'  => ['value' => 1, 'text' => '默认选择', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'warning'],
        ];
        $form->switch('is_default','默认经纪人')->states($states)->default(1);
        $form->saved(function ($from){

            event(new AgentCreated($from->model()));
        });
        return $form;

    }
}
